package flst.ipiweb.struts.action.livraison;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import flst.ipiweb.service.ServiceLivraison;
import flst.ipiweb.transverse.IPIException;
import flst.ipiweb.transverse.Relai;

public class ListRelaiAction extends Action{

	public ActionForward execute(
			ActionMapping mapping, 
			ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response)
			throws Exception {
			
			//On récupère la session
			HttpSession session = request.getSession();
			//Si l'administrateur n'est pas logué 
			//on le redirige sur le formulaire de login
			if(session.getAttribute("login") == null)
			{
				return mapping.findForward("login");
			}
		
			request.setAttribute("pageTitle", "relai.list.title");
			HashMap<String, String> onglet = new HashMap<String, String>();
			onglet.put("livraison", "active");
			request.setAttribute("onglet", onglet);
			
			ServiceLivraison sLivraison = new ServiceLivraison();
			int filter = 0;
			if(request.getParameter("type") != null)
			{
				filter = Integer.parseInt(request.getParameter("type"));
			}
			List<Relai> listRelai = new ArrayList<Relai>();
			HashMap<String, String> filtre = new HashMap<String, String>();
			try
			{
				switch(filter)
				{
					case 2:
						listRelai = sLivraison.listerRelaiByName();
						filtre.put("name", "selected");
						break;
					case 3:
						listRelai = sLivraison.listerRelaiByVille();
						filtre.put("ville", "selected");
						break;
					default:
						listRelai = sLivraison.listerRelai();
						filtre.put("numero", "selected");
						break;
				}
				
			}
			catch(IPIException e)
			{
				session.setAttribute("error",e.getMessage());
			}
			
			request.setAttribute("listRelai", listRelai);
			request.setAttribute("filtre", filtre);

			return mapping.findForward("list_relai");
		}
}
